"""
  文件读写测试
"""

import typing

from elio import core
from elio.core import File

FILE_NAME = 'test.txt'
CONTENT = '''\
山不在高，有仙则名。
水不在深，有龙则灵。
斯是陋室，惟吾德馨。
苔痕上阶绿，草色入帘青。
谈笑有鸿儒，往来无白丁。
可以调素琴，阅金经。
无丝竹之乱耳，无案牍之劳形。
南阳诸葛庐，西蜀子云亭。
孔子云：何陋之有
'''


class FileLayer(core.AutoRecycleLayer[core.Activity]):
    tc_info: core.WidgetEditor
    btn_write: core.WidgetButton
    btn_read: core.WidgetButton
    btn_exists: core.WidgetButton
    btn_del: core.WidgetButton

    async def on_create(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_create(*args, **kwargs)

    async def on_destroy(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_destroy()

    async def on_show(self):
        await super().on_show()
        await self.init_ui()

    async def on_hide(self):
        await super().on_hide()
        await self.un_init_ui()

    async def init_ui(self):
        activity = self.parent
        width, height = await activity.get_screen_size()
        self.tc_info = await core.WidgetEditor.create(activity, width - 60, 500, 30, 30)
        self.btn_write = await self.create_button('写入文件', 600, self.cb_write)
        self.btn_read = await self.create_button('读取文件', 650, self.cb_read)
        self.btn_exists = await self.create_button('文件是否存在', 700, self.cb_exists)
        self.btn_del = await self.create_button('删除文件', 750, self.cb_del)

    async def un_init_ui(self):
        await self.tc_info.destroy()
        await self.btn_write.destroy()
        await self.btn_read.destroy()
        await self.btn_exists.destroy()
        await self.btn_del.destroy()

    async def on_pause(self):
        await super().on_pause()

    async def on_resume(self):
        await super().on_resume()

    async def create_button(self, name: str, pos_y: int, cb: typing.Callable) -> core.WidgetButton:
        """
        创建按钮。
        :param name: 按钮文字。
        :param pos_y: 纵坐标。
        :param cb: 点击处理函数。
        :return: core.WidgetButton
        """
        activity = self.parent
        width, _ = await activity.get_screen_size()

        button = await core.WidgetButton.create(activity, width - 60, 50, 30, pos_y, name)
        await button.set_on_click_listener(cb)
        return button

    async def cb_write(self):
        """写入文件按钮点击的回调函数"""
        await self.tc_info.set_text('正在写入...')
        async with await File.open(self.parent, FILE_NAME, 'w') as f:
            await f.write(CONTENT)
            await self.tc_info.set_text('写入成功!')

    async def cb_read(self):
        """读取文件按钮点击的回调函数"""
        await self.tc_info.set_text('正在读取...')
        async with await File.open(self.parent, FILE_NAME) as f:
            data = await f.read()
            if data is not None:
                await self.tc_info.set_text(data)
            else:
                await self.tc_info.set_text('未获取到内容!')

    async def cb_exists(self):
        """文件存在查询按钮回调方法"""
        async with await File.open(self.parent, FILE_NAME) as f:
            b = await f.exists()
            exists = '已存在' if b else '未存在'
            await self.tc_info.set_text(f'文件 {FILE_NAME} {exists}')

    async def cb_del(self):
        """删除按钮回调方法"""
        async with await File.open(self.parent, FILE_NAME) as f:
            await f.remove()
            await self.tc_info.set_text(f'已删除 {FILE_NAME}')
